from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from options import options
from service import service


class Discus:
    def __init__(self, driver):
        self.driver = driver

    def login(self, username, password):
        self.driver.find_element(By.ID, 'ls_username').clear()
        self.driver.find_element(By.ID, 'ls_username').send_keys(username)
        self.driver.find_element(By.ID, 'ls_password').clear()
        self.driver.find_element(By.ID, 'ls_password').send_keys(password)
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em').click()
        sleep(3)

    def register(self, username='test26', password=123456, Confirm=123456, Email='test26@qq.com'):
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[4]/a').click()
        self.driver.find_element(By.ID, 'yhRzq0').clear()
        self.driver.find_element(By.ID, 'yhRzq0').send_keys(username)
        self.driver.find_element(By.ID, 'Rh58h6').clear()
        self.driver.find_element(By.ID, 'Rh58h6').send_keys(password)
        self.driver.find_element(By.ID, 'yz2omV').clear()
        self.driver.find_element(By.ID, 'yz2omV').send_keys(Confirm)
        self.driver.find_element(By.ID, 'UweHUE').clear()
        self.driver.find_element(By.ID, 'UweHUE').send_keys(Email)
        self.driver.find_element(By.XPATH, '//*[@id="registerformsubmit"]/strong').click()
        sleep(7)

    def post(self, subject, message, username, password):
        self.login(username, password)
        # 发帖
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        self.driver.find_element(By.ID, 'newspecial').click()
        self.driver.find_element(By.ID, 'subject').clear()
        self.driver.find_element(By.ID, 'subject').send_keys(subject)
        self.driver.switch_to.frame('e_iframe')
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys(message)
        self.driver.switch_to.default_content()
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(3)

    def read(self, username, password):
        self.login(username, password)
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        self.driver.find_element(By.XPATH, '//*[@id="normalthread_4"]/tr/th/a[2]').click()
        sleep(5)

    def reply(self, postmessage, username, password):
        self.login(username, password)
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        self.driver.find_element(By.XPATH, '//*[@id="normalthread_4"]/tr/th/a[2]').click()
        self.driver.find_element(By.XPATH, '//*[@id="post_reply"]/img').click()
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="postmessage"]').send_keys(postmessage)
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="postsubmit"]/span').click()
        sleep(7)

    def logout(self, username, password):
        self.login(username='admin', password=123456)
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)


if __name__ == '__main__':
    url = r'http://192.168.4.6/upload/'
    username = 'admin'
    password = 123456
    driver = webdriver.Chrome(options=options,service=service)
    driver.get(url)

    discuz = Discus(driver)
    # discuz.assert(username, password)
    # discuz.register(username = 'test26', password = 123456, Confirm=123456, Email='test26@qq.com')
    # discuz.post(subject, message, username, password)
    # discuz.read(username, password)
    # discuz.reply(postmessage, username, password)
    discuz.logout(username, password)

    driver.quit()
